package com.bci.pwtz.service.impl;

import java.text.MessageFormat;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import com.bci.pwtz.mysql.model.CompanyMessage;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.SmsSendLog;
import com.bci.pwtz.service.CompanyMessageService;
import com.bci.pwtz.service.CompanyUserService;
import com.bci.pwtz.service.MessageService;
import com.bci.pwtz.service.SmsSendLogService;
import com.bci.pwtz.service.sms.SendSmsService;

public class MessageServiceImpl implements MessageService
{
    private static final Logger logger = Logger.getLogger(MessageServiceImpl.class); 
    private CompanyMessageService companyMessageService;    
    private SendSmsService sendSmsService;
    private CompanyUserService companyUserService;
    private SmsSendLogService smsSendLogService;
    
    @Override
    public void sendNoticeSmsAndMsg(long destCompanyUserId,String formatContent,Object... params)
    {
        try
        {            
            CompanyUser destCompanyUser = companyUserService.load(destCompanyUserId);
            if(destCompanyUser == null)
            {
                logger.error("sendNoticeSmsAndMsg|发送目标对象不存在");
                return;
            }
            formatContent = MessageFormat.format(formatContent, params);
            //插入消息表
            CompanyMessage companyMessage=new CompanyMessage();
            companyMessage.setCompanyUserId(destCompanyUserId);
            companyMessage.setMessageType(1);
            companyMessage.setContent(formatContent);
            companyMessageService.add(companyMessage);           
            
            //发送短信
            String result=sendSmsService.batchSend(new String[]{destCompanyUser.getMobile()}, formatContent, null, null);
            if(!"0".equals(result)&&!"1".equals(result))
            {
                logger.error("sendNoticeSmsAndMsg|短信下发失败");
                return;
            }
            SmsSendLog smsSendLog=new SmsSendLog();
            smsSendLog.setMobile(destCompanyUser.getMobile());
            smsSendLog.setContent(formatContent);
            smsSendLogService.add(smsSendLog);            
        }
        catch(Exception e)
        {
            logger.error("sendNoticeSmsAndMsg|"+ExceptionUtils.getStackTrace(e));
        }
    }

    @Override
    public void sendAuditSmsAndMsg(long destCompanyUserId,Long auditId,String formatContent,Object... params)
    {
        try
        {
            if(auditId==null || auditId<=0)
            {
                logger.error("sendAuditSmsAndMsg|auditId参数缺失");
                return;
            }
            
            CompanyUser destCompanyUser = companyUserService.load(destCompanyUserId);
            if(destCompanyUser == null)
            {
                logger.error("sendAuditSmsAndMsg|发送目标对象不存在");
                return;
            }
            
            formatContent = MessageFormat.format(formatContent, params);
            //插入消息表
            CompanyMessage companyMessage=new CompanyMessage();
            companyMessage.setCompanyUserId(destCompanyUserId);
            companyMessage.setMessageType(1);
            companyMessage.setAuditId(auditId);
            companyMessage.setContent(formatContent);
            companyMessageService.add(companyMessage);          
            
            //发送短信
            String result=sendSmsService.batchSend(new String[]{destCompanyUser.getMobile()}, formatContent, null, null);
            if(!"0".equals(result)&&!"1".equals(result))
            {
                logger.error("sendAuditSmsAndMsg|短信下发失败");
                return;
            }
            SmsSendLog smsSendLog=new SmsSendLog();
            smsSendLog.setMobile(destCompanyUser.getMobile());
            smsSendLog.setContent(formatContent);
            smsSendLogService.add(smsSendLog);            
            
            
        }
        catch(Exception e)
        {
            logger.error("sendAuditSmsAndMsg|"+ExceptionUtils.getStackTrace(e));
        }
    }
    
    public CompanyMessageService getCompanyMessageService()
    {
        return companyMessageService;
    }

    public void setCompanyMessageService(CompanyMessageService companyMessageService)
    {
        this.companyMessageService = companyMessageService;
    }
    
    public SendSmsService getSendSmsService()
    {
        return sendSmsService;
    }
    
    public void setSendSmsService(SendSmsService sendSmsService)
    {
        this.sendSmsService = sendSmsService;
    }
    
    public CompanyUserService getCompanyUserService()
    {
        return companyUserService;
    }
    
    public void setCompanyUserService(CompanyUserService companyUserService)
    {
        this.companyUserService = companyUserService;
    }
    
    public SmsSendLogService getSmsSendLogService()
    {
        return smsSendLogService;
    }
    
    public void setSmsSendLogService(SmsSendLogService smsSendLogService)
    {
        this.smsSendLogService = smsSendLogService;
    }

}
